package com.superteam.zzl.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.os.Build;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.superteam.zzl.utils.DisplayUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.superteam.zzl.activity.R;
import com.superteam.zzl.activity.databinding.DynamicfragmentVpRvBinding;
import com.superteam.zzl.model.DynamicFragment.bean.Dynamic;
import com.superteam.zzl.viewmodel.DynamicFragment.DynamicPresenter;
import com.superteam.zzl.widget.TrendsIv;

import java.util.List;

import jp.wasabeef.glide.transformations.CropCircleTransformation;

/**
 * Created by Administrator on 2017/1/7.
 */

public class DynamicFragmentVpAllAdapter extends RecyclerViewBaseAdapter<com.superteam.zzl.model.DynamicFragment.bean.Dynamic.DataBean.TrendsListBean, DynamicFragmentVpAllAdapter.ViewHolder>{
    private DynamicPresenter presenter;
    private LayoutInflater inflater;

    public DynamicFragmentVpAllAdapter(Context context, List<com.superteam.zzl.model.DynamicFragment.bean.Dynamic.DataBean.TrendsListBean> list, DynamicPresenter presenter) {
        super(context, list);
        inflater = LayoutInflater.from(context);
        this.presenter = presenter;
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.dynamicfragment_vp_rv, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);
        Dynamic.DataBean.TrendsListBean trends = list.get(position);

        trends.setPostion(position);
        final DynamicfragmentVpRvBinding binding = holder.getBinding();
        binding.setTrends(trends);
        binding.setEvent(presenter.setEvent());
        binding.content.setText(getContent(trends));
        BitmapPool bitmapPool = Glide.get(context).getBitmapPool();
        List<Dynamic.DataBean.TrendsListBean.TagsBean> tags = trends.getTags();
        if (tags.size() != 0) {
            String tagName = "";
            for (Dynamic.DataBean.TrendsListBean.TagsBean tag : tags) {
                tagName = tagName + tag.tag_name + " ";
            }
            binding.tagName.setText(tagName);
        }

        if (trends.geoinfo.length() > 0) {
            binding.geoinfo.setVisibility(View.VISIBLE);
            binding.geoinfo.setText(trends.geoinfo);
        } else {
            binding.geoinfo.setVisibility(View.GONE);
        }

        LinearLayout linearIv = binding.linearIv;
        if (trends.action_user_count > 0) {
            linearIv.removeAllViews();
            linearIv.setVisibility(View.VISIBLE);
            for (int i = 0; i < trends.action_user_count; i++) {
                ImageView imageView = new ImageView(context);
                RecyclerView.LayoutParams params = new RecyclerView.LayoutParams(100, 100);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                    params.setMarginEnd(DisplayUtils.dipTopx(2));
                }
                imageView.setLayoutParams(params);
                Glide.with(context).load(trends.action_user_list.get(i).getUser_avatar()).bitmapTransform(new CropCircleTransformation(bitmapPool)).into(imageView);
                linearIv.addView(imageView);
            }
            TrendsIv iv = new TrendsIv(context);
            linearIv.addView(iv);
        } else {
            linearIv.setVisibility(View.GONE);
        }


        if (trends.comment_list.size() > 0) {
            binding.commentContent.removeAllViews();
            binding.commentContent.setVisibility(View.VISIBLE);
            for (int i = 0; i < trends.comment_list.size(); i++) {
                TextView tv = new TextView(context);
                Dynamic.DataBean.TrendsListBean.CommentListBean commentListBean = trends.comment_list.get(i);
                tv.setText(commentListBean.getUser_name() + ": " + commentListBean.getComment_content());
                binding.commentContent.addView(tv);
            }
        } else {
            binding.commentContent.setVisibility(View.GONE);
        }

        binding.coverImage.setPosition(position);
        binding.avatar.setPosition(position);
        Glide.with(context).load(trends.cover_image).skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.ALL).into(binding.coverImage);
        Glide.with(context).load(trends.author.avatar).bitmapTransform(new CropCircleTransformation(bitmapPool)).into(binding.avatar);
        binding.executePendingBindings();
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public String getContent(com.superteam.zzl.model.DynamicFragment.bean.Dynamic.DataBean.TrendsListBean trends) {
        String travel_start = trends.getTravel_start();
        String travel_end = trends.getTravel_end();
        StringBuilder stringBuilder = getDay(travel_start, travel_end);
        if (trends.travel_place_name != null && trends.travel_place_name.length() > 0) {
            if (stringBuilder == null) {
                stringBuilder = new StringBuilder();
            }
            stringBuilder.append("去" + trends.travel_place_name + " ");
        }

        if (trends.content != null) {
            if (stringBuilder == null) {
                stringBuilder = new StringBuilder();
            }
            stringBuilder.append(trends.content);
        }
        return stringBuilder.toString();
    }

    private StringBuilder getDay(String travelStart, String travelEnd) {
        String[] start = travelStart.split(" ")[0].split("-");
        String[] end = travelEnd.split(" ")[0].split("-");
        int Month = Integer.valueOf(end[1]) - Integer.valueOf(start[1]);
        int day = Integer.valueOf(end[2]) - Integer.valueOf(start[2]);
        StringBuilder stringBuilder = null;

        if (day > 0 || Month > 0) {
            stringBuilder = new StringBuilder(start[1] + "月" + start[2] + "日 ");
            if (Month > 0) {
                if (day < 0) {
                    switch (Month) {
                        case 1:
                        case 3:
                        case 5:
                        case 7:
                        case 8:
                        case 10:
                        case 12:
                            day = Integer.valueOf(end[2]) + 31 - Integer.valueOf(start[2]);
                            break;
                        case 2:
                            Integer integer = Integer.valueOf(start[0]);
                            if (integer % 4 == 0 && integer % 100 != 0 || integer % 400 == 0) {
                                day = Integer.valueOf(end[2]) + 29 - Integer.valueOf(start[2]);
                            } else {
                                day = Integer.valueOf(end[2]) + 28 - Integer.valueOf(start[2]);
                            }
                        case 4:
                        case 6:
                        case 9:
                        case 11:
                            day = Integer.valueOf(end[2]) + 30 - Integer.valueOf(start[2]);
                    }
                } else {
                    stringBuilder.append(Month + "个月 ");
                }
            }

            if (day > 0) {
                stringBuilder.append(day + "天 ");
            }
        }
        return stringBuilder;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private DynamicfragmentVpRvBinding db;

        public ViewHolder(View itemView) {
            super(itemView);
            db = DataBindingUtil.bind(itemView);
        }

        public DynamicfragmentVpRvBinding getBinding() {
            return db;
        }
    }
}
